/**
 * Copyright (c) 2017-2024 Nop Platform. All rights reserved.
 * Author: canonical_entropy@163.com
 * Blog:   https://www.zhihu.com/people/canonical-entropy
 * Gitee:  https://gitee.com/canonical-entropy/nop-entropy
 * Github: https://github.com/entropy-cloud/nop-entropy
 */
package com.gitee.wsl.struct.table.tree

import com.gitee.wsl.func.consumer.Consumer
import com.gitee.wsl.struct.table.ICell
import com.gitee.wsl.struct.table.IRowView
import com.gitee.wsl.struct.tree.AbstractMutableNodeElement


/**
 * 描述节点以及子节点的相对位置，占据的单元格空间大小等
 */
class TreeCell(override var value: Any?,  val childPos: TreeCellChildPosition) : AbstractMutableNodeElement<Any?,TreeCell>(value), /*ITreeStructure,*/ ICell{
     override var id: String? = null
     override var styleId: String? = null
     var model: Any? = null

     override var comment: String? = null

    override var row: IRowView? = null

    override var isFrozen: Boolean = false

    override var rowOffset: Int = -1

    override var colOffset: Int = -1

    /**
     * 父节点以及所有子节点构成的区域整体的colSpan。由calcBbox函数负责初步计算, 由adjustBbox负责修正。
     */
     var bboxWidth = 0

     var bboxHeight = 0

     override var mergeAcross = 0
     override var mergeDown = 0
     var rowIndex = -1
     var colIndex = -1

    /**
     * 树节点所处的层次，顶层的treeLevel为0，子层加1
     */
     var treeLevel = 0

    /**
     * 最底层的叶子节点的序号，从0开始
     */
     var leafIndex = 0

    override val formula: String?
        get() = null

    override fun cloneInstance(): TreeCell {
        val ret = TreeCell(
            value, childPos
        )
        ret.id = id
        ret.model = model
        ret.styleId = styleId
        ret.comment = comment
        ret.bboxWidth = bboxWidth
        ret.bboxHeight = bboxHeight
        ret.mergeAcross = mergeAcross
        ret.mergeDown = mergeDown
        ret.rowIndex = rowIndex
        ret.colIndex = colIndex
        ret.leafIndex = leafIndex
        ret.treeLevel = treeLevel
        return ret
    }

    fun forEachLeaf(consumer: Consumer<TreeCell?>) {
        if (children.isEmpty()) {
            consumer.accept(this)
        } else {
            for (child in children) {
                (child as TreeCell).forEachLeaf(consumer)
            }
        }
    }

    /**
     * 虚拟节点本身不占据空间，它所占据的空间由子节点决定，如果子节点为空，则实际上该节点可以被删除。
     */
    val isVirtual: Boolean
        get() = childPos == TreeCellChildPosition.hor || childPos == TreeCellChildPosition.ver

    fun addChild(cell: TreeCell) {
        require(cell.parent == null)
        cell.parent = this
        children.add(cell)
    }

    fun hasChild(): Boolean {
        return children.isNotEmpty()
    }

    override val colSpan: Int
        get() = mergeAcross + 1

    override val rowSpan: Int
        get() = mergeDown + 1


    val endColIndex: Int
        get() = colIndex + colSpan

    val endRowIndex: Int
        get() = rowIndex + rowSpan

    fun incWidth(deltaW: Int) {
        this.mergeAcross += deltaW
        this.bboxWidth += deltaW
    }

    fun incHeight(deltaH: Int) {
        this.mergeDown += deltaH
        this.bboxHeight += deltaH
    }

    override fun self(): TreeCell = this

    override fun createNew(value: Any?): TreeCell = TreeCell(value,TreeCellChildPosition.hor)

}